using System;
using System.Collections.Generic;
using System.Text;
using TelnetExpect;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;

namespace Winform1
{
    class RtfTbAnsiConsole : AnsiConsoleBase2
    {
        RichTextBox m_rtb;

        public RtfTbAnsiConsole(Stream peer, RichTextBox rtb)
            : base(peer)
        {
            m_rtb = rtb;
        }

        protected override void OnAnsiCommand(char command, int[] vars)
        {
            Action_OnAnsiCommand d = base.OnAnsiCommand;
            if (!m_rtb.InvokeRequired) { // unlikely
                d.Invoke(command, vars);
            } else {
                m_rtb.BeginInvoke(d, command, vars);
            }
        }

        delegate void Action_OnAnsiCommand(char command, int[] vars);

        protected override void OnText(byte[] text, int offset, int count)
        {
            Encoding enc = Encoding.ASCII;
            string str = enc.GetString(text, offset, count);
            OnText(str);
        }

        internal void OnText(string str)
        {
            Trace.WriteLine("OnText gonna append: " + TelnetBaseStream.UtilityShowControlChars(str));
            Action<string> d = delegate(string strP) {
                this.m_rtb.AppendText(strP);
            };
            if (m_rtb.InvokeRequired) {
                m_rtb.BeginInvoke(d, str);
            } else { // likely not used
                d(str);
            }
        }

        //--------
        protected override void EraseInLine(AnsiConsoleBase2.ClearAction action)
        {
            Trace.WriteLine("notimpl ANSI command: '" + "EraseInLine" + "'");
        }

        protected override void SetCursorPosition(int row, int column)
        {
            Trace.WriteLine("notimpl ANSI command: '" + "SetCursorPosition" + "'");
        }

        //--
        protected override void SetForegroundColor(ConsoleColor color)
        {
            Trace.WriteLine("notimpl ANSI command: '" + "SetForegroundColor" + "'");
        }

        protected override void SetBackgroundColor(ConsoleColor color)
        {
            Trace.WriteLine("notimpl ANSI command: '" + "SetBackgroundColor" + "'");
        }

        protected override void UpdateColors(string debugCommandName)
        {
            Trace.WriteLine("notimpl ANSI command: '" + "UpdateColors" + "'");
        }
    }
}
